package luke4fun.wheresmymoney.adapter.list.impl;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.ColorChooserAdapter;
import luke4fun.wheresmymoney.adapter.list.BasicAdapter;
import luke4fun.wheresmymoney.bean.CategoryBean;
import luke4fun.wheresmymoney.common.CommonConstants;
import luke4fun.wheresmymoney.db.AccDBHelper;
import luke4fun.wheresmymoney.db.CategoryDAO;
import luke4fun.wheresmymoney.db.DBConstants;
import luke4fun.wheresmymoney.domain.Category;
import luke4fun.wheresmymoney.viewholder.CategoryMgrListViewHolder;


public class CategoryMgrListAdapter extends BasicAdapter<CategoryBean> {
	
	private List<CategoryBean> dataList;
	private LayoutInflater inflater;
	private AccDBHelper dbHelper;
	private Activity activity;
	private CategoryDAO categoryDAO = CategoryDAO.getInstance();

	public CategoryMgrListAdapter(Context context) {
		this.inflater = LayoutInflater.from(context);
		this.activity = (Activity) context;
		dataList = new ArrayList<CategoryBean>();
	}
	
	@Override
	public int getCount() {
		return dataList == null ? 0 : dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CategoryMgrListViewHolder holder;
		
		// set the view holder for reuse
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.category_mgr_list, null);
			holder = new CategoryMgrListViewHolder();
			holder.categoryId = (TextView) convertView.findViewById(R.id.category_id);
			holder.colorChooser = (Spinner) convertView.findViewById(R.id.color_chooser);
			holder.oriCategory = (TextView) convertView.findViewById(R.id.ori_category);
			holder.category = (EditText) convertView.findViewById(R.id.category_txt);
			holder.deleteBtn = (ImageButton) convertView.findViewById(R.id.delete);
			
			convertView.setTag(holder);
		} else {
			holder = (CategoryMgrListViewHolder) convertView.getTag();
		}
		
		CategoryBean bean = dataList.get(position);
		holder.categoryId.setText(bean.getId() + "");
		holder.oriCategory.setText(bean.getName());
		holder.category.setText(bean.getName());
		holder.category.setOnFocusChangeListener(createOnFocusChangeListener());
		
		
		holder.colorChooser.setAdapter(new ColorChooserAdapter(activity, holder.colorChooser, CommonConstants.COLOR, true));
		((View) holder.colorChooser.getParent()).setBackgroundColor(bean.getColor());
		holder.colorChooser.setSelection(CommonConstants.COLOR.indexOf(bean.getColor()), true);
		
		holder.deleteBtn.setOnClickListener(createOnClickListener());
		
		return convertView;
	}

	@Override
	public void updateList(List<CategoryBean> list) {
		this.dataList = list;
		notifyDataSetChanged();
	}
	
	private View.OnFocusChangeListener createOnFocusChangeListener() {
		return new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					LinearLayout lLayout = (LinearLayout) v.getParent();
					TextView categoryId = (TextView) lLayout.findViewById(R.id.category_id);
					TextView oriCategory = (TextView) lLayout.findViewById(R.id.ori_category);
					
					if (!oriCategory.getText().toString().equals(((EditText) v).getText().toString())) {
						dbHelper = new AccDBHelper(activity);
						SQLiteDatabase db = dbHelper.getWritableDatabase();
						ContentValues cv = new ContentValues();
						cv.put(DBConstants.CATEGORY_NAME, ((EditText) v).getText().toString());
						db.update(DBConstants.TABLE_CATEGORY, cv, DBConstants.CATEGORY_ID + "=?", new String[] {categoryId.getText().toString()});
						db.close();
						dbHelper.close();
						Toast.makeText(activity, "category updated", Toast.LENGTH_SHORT).show();
					}
				}
			}
		};
	}
	
	private View.OnClickListener createOnClickListener() {
		return new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LinearLayout lLayout = (LinearLayout) v.getParent();
				long categoryId = Long.parseLong( ((TextView) lLayout.findViewById(R.id.category_id)).getText().toString() );

				dbHelper = new AccDBHelper(activity);
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				categoryDAO.deleteCategory(db, categoryId);
				List<Category> list = categoryDAO.queryCategory(db, null);
				db.close();
				dbHelper.close();
				
				List<CategoryBean> beanList = new ArrayList<CategoryBean>();
				for (Category c : list) {
					CategoryBean bean = new CategoryBean();
					bean.setId(c.getId());
					bean.setName(c.getName());
					bean.setColor(c.getColor());
					
					beanList.add(bean);
				}
				updateList(beanList);
			}
		};
	}
}
